<?php
    if (! class_exists('NaturalExtensionAjax_SMMBuilder')) {
        class NaturalExtensionAjax_SMMBuilder {
            private $smm_category;

            public function __construct() {
                $this->smm_category = get_terms('link_category', array(
                    'slug'          => 'social-media',
                    'hide_empty'    => false,
                    'fields'        => 'ids'
                ));
            }

            public function build_link() {
                $link = array(
                    'link_name'     => $this->getData($_POST['link_name'], 'Link Name'),
                    'link_url'      => $this->getData($_POST['link_url'], 'http://dymmy.com'),
                    'link_target'   => '_blank',
                    'link_category' => $this->smm_category[0]
                );
                $include_icons  = $this->getData($_POST['include_icons'], 'false');
                $link_image     = $this->getData($_POST['link_image'], 'false');
                $message = 'success';

                if ($include_icons == 'true') {
                    $link['link_image'] = $link_image;
                }

                try {
                    wp_insert_link($link);
                } catch (Exception $e) {
                    $message = $e->getMessage();
                }

                exit($message);
            }

            public function getData($data, $default) {
                return isset($data) ? $data : $default;
            }
        }

        $natext_ajax_smm_builder = new NaturalExtensionAjax_SMMBuilder();

        add_action('wp_ajax_nopriv_NatExtAjax_BuildSMM', array($natext_ajax_smm_builder, 'build_link'));
        add_action('wp_ajax_NatExtAjax_BuildSMM', array($natext_ajax_smm_builder, 'build_link'));
    }
?>